package com.chif.mapreduce.compress;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.io.compress.BZip2Codec;
import org.apache.hadoop.io.compress.CompressionCodec;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;


public class WordCountDriver {

    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {

        //1 获取配置信息以及获取job对象
        Configuration conf = new Configuration();
        //指定hadoop运行时在本地的临时工作目录
        conf.set("hadoop.tmp.dir", "D:/tmp/mapreduce_tmp_cache");

        //开启map端输出压缩
        conf.setBoolean("mapreduce.map.output.compress",true);

        //设置map端输出压缩方式
        conf.setClass("mapreduce.map.output.compress.codec", BZip2Codec.class, CompressionCodec.class);

        Job job = Job.getInstance(conf);

        //2 关联本Driver程序的jar
        job.setJarByClass(WordCountDriver.class);

        //3 关联Mapper和Reduce的class
        job.setMapperClass(WordCountMapper.class);
        job.setReducerClass(WordCountReducer.class);

        //4 设置Mapper输出的kv类型
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(IntWritable.class);

        // 5 设置最终输出kv类型
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);


        //6 设置输入和输出路径
        FileInputFormat.setInputPaths(job,new Path("C:\\Users\\Chef Liu\\Desktop\\Big\\hadoop\\input4"));
        FileOutputFormat.setOutputPath(job,new Path("C:\\Users\\Chef Liu\\Desktop\\Big\\hadoop\\output"));

        //设置reduce端输出压缩开启
        FileOutputFormat.setCompressOutput(job,true);

        //设置压缩的方式
        FileOutputFormat.setOutputCompressorClass(job, BZip2Codec.class);

        //7 提交job
        boolean result = job.waitForCompletion(true);

        System.exit(result ? 0 : 1);
    }
}
